package com.pp.demo.module.user.converter;

import com.pp.demo.module.user.dto.UserDTO;
import com.pp.demo.module.user.entity.User;
import com.pp.demo.module.user.param.UserCreateParam;
import com.pp.demo.module.user.vo.UserVO;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Named;
import org.mapstruct.factory.Mappers;

import java.util.List;

@Mapper
public interface UserConverter {
    UserConverter INSTANCE = Mappers.getMapper(UserConverter.class);

    // Entity → DTO
    UserDTO toDTO(User entity);
    // DTO → VO
    @Mapping(source = "name", target = "username")
    @Mapping(source = "sex", target = "sex", qualifiedByName = "sexConvert")
    UserVO toVO(UserDTO dto);

    // Param -> Entity
    User toEntity(UserCreateParam param);

    // List转换
    List<UserDTO> toDTOList(List<User> entities);
    List<UserVO> toVOList(List<UserDTO> dtos);

    @Named("sexConvert")
    default String convertSex(Integer sex) {
        if (sex == null) {
            return "未知"; // 处理空值
        }
        return sex == 0 ? "男" : "女";
    }
}
